package cn.ante.mq;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class TranProduct {
    public static void main(String[] args) throws Exception {
        TransactionMQProducer producer = new TransactionMQProducer("tran_producer_group");
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 指定监听器
        producer.setTransactionListener(new MyTranListener());
        // 启动生产者
        producer.start();
        // 发送事务消息
        Message message = new Message("tran_topic", "tran_tags", "发送了事务消息！".getBytes(StandardCharsets.UTF_8));
        // 发送事务消息
        TransactionSendResult result = producer.sendMessageInTransaction(message, null);
        System.out.println(result);
        // 关闭生产者
        producer.shutdown();
    }
}
